﻿Imports Microsoft.VisualBasic
Imports System.Data.OleDb
<Serializable>
Public Class Product
    Public Property RowNumber As Integer
    Public Property ID As Integer
    Public Property Name As String
    Public Property Desc As String
    Public Property ImagePath As String
    Public Property Amount As Double
    Public Property Qty As Integer = 1
    Public Property CanPersonalize As Boolean
    Public Property Personalize As Boolean = False
    Public Property Comment As String
End Class

Public Class CartProducts
    Public ProductCollection As List(Of Product)
    Public Sub New()
        ProductCollection = New List(Of Product)
    End Sub
    Public Function GetItemCount() As Integer
        Return ProductCollection.Sum(Function(x) x.Qty)
    End Function
    Public Function GetCartList() As List(Of Product)
        Return ProductCollection
    End Function
    Public Function GetCartSum() As Double
        Return ProductCollection.Sum(Function(x) x.Qty * x.Amount)
    End Function
    Public Sub EmptyCart()
        ProductCollection = New List(Of Product)
    End Sub
    Public Sub AddToCart(ByVal newitem As Product)
        'If item is already in cart, then increment Qty
        If ProductCollection.Where(Function(x) x.ID = newitem.ID).Count > 0 AndAlso Not newitem.CanPersonalize Then
            ProductCollection.Where(Function(x) x.ID = newitem.ID).First().Qty += Math.Max(newitem.Qty, 1)
        Else
            newitem.Qty = Math.Max(newitem.Qty, 1)
            newitem.Amount = Math.Max(newitem.Amount, 1)
            newitem.RowNumber = ProductCollection.Count + 1
            ProductCollection.Add(newitem)
        End If
    End Sub
    Public Sub RemoveItem(ByVal row As Integer)
        If row - 1 < ProductCollection.Count Then
            ProductCollection.RemoveAt(row - 1)
            'reseed the row num count
            Dim int As Integer = 1
            For Each prod As Product In ProductCollection
                prod.RowNumber = int
                int += 1
            Next
        End If
    End Sub
    Public Sub UpdateItem(ByVal upitem As Product)
        If ProductCollection.Where(Function(x) x.RowNumber = upitem.RowNumber).Count > 0 Then
            'Update the item
            Dim item As Product = ProductCollection.Where(Function(x) x.RowNumber = upitem.RowNumber).First()
            item.Qty = upitem.Qty
            item.Personalize = upitem.Personalize
            item.Comment = upitem.Comment
        End If
    End Sub
    Public Function CompleteOrder(ByVal CustomerID As Integer, ByVal CardID As Integer) as Integer
        Dim orderid As Integer = CreateOrder(CustomerID)
        CreateLineItems(orderid)
        ProcessPayment(orderid, CardID, GetCartSum())
        Return orderid
    End Function
    Private Sub ProcessPayment(ByVal orderid As Integer, ByVal cardid As Integer, ByVal amount As Double)
        Dim accessCon As New OleDbConnection(ConfigurationManager.ConnectionStrings("GotPinkDB").ConnectionString)
        accessCon.Open()
        Dim sql As String = "INSERT INTO Payments ( OrderID, CardID, Amount) " & _
                            "Values(@OrderID, @CardID, @Amount)"
        Dim accessCom As New OleDbCommand(sql, accessCon)
        Dim param1 As OleDbParameter = accessCom.Parameters.Add("@OrderID", OleDbType.Integer)
        Dim param2 As OleDbParameter = accessCom.Parameters.Add("@CardID", OleDbType.Integer)
        Dim param3 As OleDbParameter = accessCom.Parameters.Add("@Amount", OleDbType.Double)
        param1.Value = orderid
        param2.Value = cardid
        param3.Value = amount

        Try
            accessCom.ExecuteScalar()
        Catch ex As Exception

        Finally
            accessCom.Dispose()
            accessCon.Close()
        End Try
    End Sub
    Private Function CreateOrder(ByVal CustomerID As Integer) As Integer
        Dim newid As Integer
        Dim accessCon As New OleDbConnection(ConfigurationManager.ConnectionStrings("GotPinkDB").ConnectionString)
        accessCon.Open()
        Dim sql As String = "INSERT INTO Orders ( OrderType, CustomerID, OrderDate) " & _
                            "Values('Regular', @CustomerID, @OrderDate)"
        Dim accessCom As New OleDbCommand(sql, accessCon)
        Dim param1 As OleDbParameter = accessCom.Parameters.Add("@CustomerID", OleDbType.Integer)
        Dim param2 As OleDbParameter = accessCom.Parameters.Add("@OrderDate", OleDbType.Date)
        param1.Value = CustomerID
        param2.Value = Now

        Try
            accessCom.ExecuteScalar()
            Dim query As String = "SELECT @@IDENTITY"
            Dim cmd As OleDbCommand = New OleDbCommand(query, accessCon)
            newid = CType(cmd.ExecuteScalar(), Integer)
        Catch ex As Exception
            newid = 0
        Finally
            accessCom.Dispose()
            accessCon.Close()
        End Try
        Return newid
    End Function
    Private Sub CreateLineItems(ByVal orderid As Integer)
        For Each prod In ProductCollection
            CreateLine(orderid, prod)
        Next
    End Sub
    Private Sub CreateLine(ByVal orderid As Integer, ByVal prod As Product)
        Dim accessCon As New OleDbConnection(ConfigurationManager.ConnectionStrings("GotPinkDB").ConnectionString)
        accessCon.Open()
        Dim sql As String = "INSERT INTO OrderLine ( OrderID, ProductID, Qty, Amount, Personalization, Comment ) " & _
                            "Values (@OrderID, @ProductID, @Qty, @Amount, @Personalization, @Comment)"
        Dim accessCom As New OleDbCommand(sql, accessCon)
        Dim param1 As OleDbParameter = accessCom.Parameters.Add("@OrderID", OleDbType.Integer)
        Dim param2 As OleDbParameter = accessCom.Parameters.Add("@ProductID", OleDbType.Integer)
        Dim param3 As OleDbParameter = accessCom.Parameters.Add("@Qty", OleDbType.Integer)
        Dim param4 As OleDbParameter = accessCom.Parameters.Add("@Amount", OleDbType.Double)
        Dim param5 As OleDbParameter = accessCom.Parameters.Add("@Personalization", OleDbType.Boolean)
        Dim param6 As OleDbParameter = accessCom.Parameters.Add("@Comment", OleDbType.VarChar, 255)
        param1.Value = orderid
        param2.Value = prod.ID
        param3.Value = prod.Qty
        param4.Value = prod.Amount
        param5.Value = prod.Personalize
        param6.Value = prod.Comment

        Try
            accessCom.ExecuteScalar()
        Catch ex As Exception

        Finally
            accessCom.Dispose()
            accessCon.Close()
            End Try
    End Sub
End Class
Public Class UserCart
    Public Shared Function GetCart() As List(Of Product)
        Return CType(HttpContext.Current.Session("MyCart"), CartProducts).GetCartList()
    End Function
    Public Shared Sub AddtoCart(ByVal newitem As Product)
        CType(HttpContext.Current.Session("MyCart"), CartProducts).AddToCart(newitem)
    End Sub
    Public Shared Sub RemoveItem(ByVal RowNumber As Integer)
        CType(HttpContext.Current.Session("MyCart"), CartProducts).RemoveItem(RowNumber)
    End Sub
    Public Shared Sub UpdateItem(ByVal upitem As Product)
        CType(HttpContext.Current.Session("MyCart"), CartProducts).UpdateItem(upitem)
    End Sub
    Public Shared Function CompleteOrder(ByVal customerid As Integer, ByVal cardid As Integer) As Integer
        Return CType(HttpContext.Current.Session("MyCart"), CartProducts).CompleteOrder(customerid, cardid)
    End Function
    Public Shared Sub EmptyCart()
        CType(HttpContext.Current.Session("MyCart"), CartProducts).EmptyCart()
    End Sub
End Class
Public Class ReceiptCart
    Public Shared Function GetCart() As List(Of Product)
        Return CType(HttpContext.Current.Session("Checkedout"), CartProducts).GetCartList()
    End Function
    Public Shared Sub AddtoCart(ByVal newitem As Product)
        CType(HttpContext.Current.Session("Checkedout"), CartProducts).AddToCart(newitem)
    End Sub
    Public Shared Sub RemoveItem(ByVal RowNumber As Integer)
        CType(HttpContext.Current.Session("Checkedout"), CartProducts).RemoveItem(RowNumber)
    End Sub
    Public Shared Sub UpdateItem(ByVal upitem As Product)
        CType(HttpContext.Current.Session("Checkedout"), CartProducts).UpdateItem(upitem)
    End Sub
    Public Shared Function CompleteOrder(ByVal customerid As Integer, ByVal cardid As Integer) As Integer
        Return CType(HttpContext.Current.Session("Checkedout"), CartProducts).CompleteOrder(customerid, cardid)
    End Function
    Public Shared Sub EmptyCart()
        CType(HttpContext.Current.Session("Checkedout"), CartProducts).EmptyCart()
    End Sub
End Class
